﻿using DevComponents.DotNetBar;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;
using WorkStation.Logic;

namespace WorkStation.UI.Controls
{
    public partial class DDControl : UserControl
    {
        private DDMotor _ddMotor;
        public DDControl()
        {
            InitializeComponent();
        }
        public void SetMotor(DDMotor motor)
        {
            this._ddMotor = motor;
        }
        private void buttonX1_Click(object sender, EventArgs e)
        {

        }

        private async void DD_Click(object sender, EventArgs e)
        {
            string errInfo = "";
            (sender as ButtonX).Enabled = false;
            if ((sender as ButtonX).Text.Equals("回原点"))
            {
                (sender as ButtonX).Enabled = await Task.Run(() =>
                {
                    if (!_ddMotor.GoHome(out errInfo))
                    {
                        if (!string.IsNullOrEmpty(errInfo))
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, errInfo, true);
                            }));
                            return true;
                        }
                    }
                    else
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            SystemInfo.ShowToastInfo(this, "回零完成!", false);
                        }));
                    }
                    return true;
                });
            }
            else if ((sender as ButtonX).Text.Equals("报警复位"))
            {
                _ddMotor.AlarmReset();
            }
            else if ((sender as ButtonX).Text.Equals("转动一格"))
            {
                (sender as ButtonX).Enabled = await Task.Run(() =>
                {
                    if (!_ddMotor.RotateOnce(out errInfo))
                    {
                        if (!string.IsNullOrEmpty(errInfo))
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, errInfo, true);
                            }));
                            return true;
                        }
                    }
                    else
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            SystemInfo.ShowToastInfo(this, "转动一格完成!", false);
                        }));
                    }
                    return true;
                });
            }
            else if ((sender as ButtonX).Text.Equals("读取当前位置"))
            {

            }
        } 
        public void DDRotateOnce()
        {

        }

        private void sB_OnOff_ValueChanged(object sender, EventArgs e)
        {
            if(sB_OnOff.Value)
            {
                _ddMotor.ServoOn();
            }
            else
            {
                _ddMotor.ServoOff();
            }
        }
    }
}
